import java.util.Arrays;

public class TimeTravellingTour {
	public long determineCost(int n, int[] cities, String[] roads) {
		int[][] graph = new int[n][n];
		StringBuilder roadsDescription = new StringBuilder();
		for (String line : roads)
			roadsDescription.append(line);
		String[] roadDescriptions = roadsDescription.toString().split(" ");
		for (String roadDescription : roadDescriptions) {
			String[] tokenizedRoadDescription = roadDescription.split(",");
			int from = Integer.parseInt(tokenizedRoadDescription[0]);
			int to = Integer.parseInt(tokenizedRoadDescription[1]);
			int cost = Integer.parseInt(tokenizedRoadDescription[2]);
			graph[from][to] = graph[to][from] = cost;
		}
		boolean[] reachable = new boolean[n];
		reachable[0] = true;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (reachable[j]) {
					for (int k = 0; k < n; k++) {
						if (graph[j][k] != 0)
							reachable[k] = true;
					}
				}
			}
		}
		for (int city : cities) {
			if (!reachable[city])
				return -1;
		}
		int m = cities.length;
		long[][][] res = new long[m][m][n];
		boolean[] visited = new boolean[n];
		for (int i = m - 1; i >= 0; i--) {
			for (int j = i; j < m; j++) {
				Arrays.fill(res[i][j], Long.MAX_VALUE);
				if (i == j)
					res[i][j][cities[i]] = 0;
				else {
					for (int k = 0; k < n; k++) {
						for (int l = i; l < j; l++)
							res[i][j][k] = Math.min(res[i][j][k], res[i][l][k] + res[l + 1][j][k]);
					}
				}
				Arrays.fill(visited, false);
				for (int k = 0; k < n; k++) {
					int minIndex = -1;
					long minValue = Long.MAX_VALUE;
					for (int l = 0; l < n; l++) {
						if (res[i][j][l] < minValue && !visited[l]) {
							minIndex = l;
							minValue = res[i][j][l];
						}
					}
					if (minIndex == -1)
						break;
					visited[minIndex] = true;
					for (int l = 0; l < n; l++) {
						if (graph[minIndex][l] != 0)
							res[i][j][l] = Math.min(res[i][j][l], minValue + graph[minIndex][l]);
					}
				}
			}
		}
		return res[0][m - 1][0];
	}


// BEGIN CUT HERE
	public static void main(String[] args) {
		if (args.length == 0) {
			TimeTravellingTourHarness.run_test(-1);
		} else {
			for (int i=0; i<args.length; ++i)
				TimeTravellingTourHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class TimeTravellingTourHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(long expected, long result) { return expected == result; }
	static String formatResult(long res) {
		return String.format("%d", res);
	}
	
	static int verifyCase(int casenum, long expected, long received) { 
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch(casenum) {
		case 0: {
			int N                     = 5;
			int[] cities              = {2,3,2,4};
			String[] roads            = {"0,2,4 0,1,2 2,1,2 1,3,3 4,0,4"};
			long expected__           = 13L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}
		case 1: {
			int N                     = 3;
			int[] cities              = {1,0,1};
			String[] roads            = {"0,2,1"," 2",",1,5"};
			long expected__           = 12L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}
		case 2: {
			int N                     = 3;
			int[] cities              = {2};
			String[] roads            = {"0,1,2"};
			long expected__           = -1L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}
		case 3: {
			int N                     = 6;
			int[] cities              = {4, 1, 3, 2};
			String[] roads            = {"0,1,5 0,2,5 0,5,2 2,3,5 2,4,2 3,4,4 3,5,1"};
			long expected__           = 19L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}

		// custom cases

/*      case 4: {
			int N                     = ;
			int[] cities              = ;
			String[] roads            = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}*/
/*      case 5: {
			int N                     = ;
			int[] cities              = ;
			String[] roads            = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}*/
/*      case 6: {
			int N                     = ;
			int[] cities              = ;
			String[] roads            = ;
			long expected__           = L;

			return verifyCase(casenum, expected__, new TimeTravellingTour().determineCost(N, cities, roads));
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
